package top.codedance.iotp.common.entity;

import top.codedance.iotp.common.protocol.CommonDecoderCodec;
import top.codedance.iotp.common.util.LongIdGenerator;

import java.io.Serializable;

public class CommonEntity implements Serializable {
    private byte head;
    private int packageLength = 0;
    private short command;
    private long version;
    private byte[] body;

    public CommonEntity() {
        this.head = CommonDecoderCodec.HEAD_TAG;
        LongIdGenerator longIdGenerator = new LongIdGenerator();
        this.version = longIdGenerator.generate();
    }

    public CommonEntity(long version) {
        this();
        this.version = version;
    }

    public byte getHead() {
        return head;
    }

    public void setHead(byte head) {
        this.head = head;
    }

    public int getPackageLength() {
        return packageLength;
    }

    public void setPackageLength(int packageLength) {
        this.packageLength = packageLength;
    }

    public short getCommand() {
        return command;
    }

    public void setCommand(short command) {
        this.command = command;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
        this.build();
    }

    public CommonEntity build() {
        int pl = Byte.SIZE / 8 + Integer.SIZE / 8 + Short.SIZE / 8 + Long.SIZE / 8;
        if (body != null) {
            pl += body.length;
        }
        this.setPackageLength(pl);
        return this;
    }
}
